package com.affaris.controller;


import com.affaris.common.QueryPageParam;
import com.affaris.common.Result;
import com.affaris.entity.Meetings;
import com.affaris.service.MeetingsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lucas
 * @since 2023-05-15
 */
@RestController
@RequestMapping("/meetings")
public class MeetingsController {

    @Autowired
    private MeetingsService meetingsService;
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Meetings meetings) {
        return meetingsService.save(meetings) ? Result.suc() : Result.fail();
    }

    //删除
    @GetMapping("/delete")
    public Result delete(@RequestParam String id) {

        return meetingsService.removeById(id) ? Result.suc() : Result.fail();
    }

    //更新
    @PostMapping("/update")
    public Result update(@RequestBody Meetings meetings) {
        return meetingsService.updateById(meetings) ? Result.suc() : Result.fail();
    }

    //查询
    @PostMapping("/page")
    public Result page(@RequestBody QueryPageParam queryPageParam) {
        HashMap param = queryPageParam.getParam();
        String name = (String) param.get("name");
        String organization = (String) param.get("organization");

        Page<Meetings> page = new Page();
        page.setCurrent(queryPageParam.getPageNum());
        page.setSize(queryPageParam.getPageSize());

        LambdaQueryWrapper<Meetings> lambdaQueryWrapper = new LambdaQueryWrapper();
        if (StringUtils.isNotBlank(name) && !"null".equals(name)) {
            lambdaQueryWrapper.like(Meetings::getName, name);
        }
        if (StringUtils.isNotBlank(organization) && !"null".equals(organization)) {
            lambdaQueryWrapper.eq(Meetings::getOrganization, organization);
        }

        IPage result = meetingsService.page(page, lambdaQueryWrapper);
        return Result.suc(result.getRecords(), result.getTotal());
    }



}
